package com.weibo.tencent;

import com.snda.lib.util.MiscHelper;

public class QWeibo {

	private static QWeibo instance = null;
	
	private String mstrCallBackUrl = "";
	
	private String mstrConsumeKey = "6ad6fdb0a1c84834ac006ce41163086d";
	private String mstrConsumeSecret = "d6edfdb3152f7dd18f6b45af1c761324";

	private String mstrRequestToken;
	private String mstrRequestTokenSecret;

	private String mstrAccessToken;
	private String mstrAccessTokenSecret;

	private QWeibo() {
	};
	
	public void setCallBackUrl(String strCallBackUrl){
		mstrCallBackUrl = strCallBackUrl;
	}


	public static synchronized QWeibo getInstance() {
		if (instance == null)
			instance = new QWeibo();

		return instance;
	}

	public String getConsumeKey() {
		return mstrConsumeKey;
	}

	public void setConsumeKey(String strConsumeKey) {
		mstrConsumeKey = strConsumeKey;
	}

	public String getConsumeSecret() {
		return mstrConsumeSecret;
	}

	public void setConsumeSecret(String strConsumeSecret) {
		mstrConsumeSecret = strConsumeSecret;
	}

	public String getRequestToken() {
		return mstrRequestToken;
	}

	public String getRequestTokenSecret() {
		return mstrRequestTokenSecret;
	}

	public void setRequestTokenSecret(String strRequestTokenSecret) {
		this.mstrRequestTokenSecret = strRequestTokenSecret;
	}

	public String getAccessToken() {
		return mstrAccessToken;
	}

	public void setAccessToken(String strAccessToken) {
		this.mstrAccessToken = strAccessToken;
	}

	public String getAccessTokenSecret() {
		return mstrAccessTokenSecret;
	}

	public void setAccessTokenSecret(String strAccessTokenSecret) {
		this.mstrAccessTokenSecret = strAccessTokenSecret;
	}

	/**
	 * @desc 请求Request Token
	 * @return
	 */
	public String requestRequestToken() {

		String strResponse = null;
		QWeiboSyncApi api = new QWeiboSyncApi();
		api.setCallBackUrl(mstrCallBackUrl);
		strResponse = api.getRequestToken(mstrConsumeKey, mstrConsumeSecret);
		if (MiscHelper.isEmpty(strResponse)) {
			return null;
		}

		String[] tokenArray = strResponse.split("&");
		if (tokenArray.length < 2) {
			return null;
		}

		String strTokenKey = tokenArray[0];
		String strTokenSecrect = tokenArray[1];
		String[] token1 = strTokenKey.split("=");
		if (token1.length < 2) {
			return null;
		}

		String[] token2 = strTokenSecrect.split("=");
		if (token2.length < 2) {
			return null;
		}

		mstrRequestToken = token1[1];
		mstrRequestTokenSecret = token2[1];

		return mstrRequestToken;
	}

	/**
	 * @desc 请求AccessToken
	 * @return
	 */
	public String requestAccessToken(String strOauthVerifier) {

		QWeiboSyncApi qWeibo = new QWeiboSyncApi();
		String strAccessToken = qWeibo.getAccessToken(mstrConsumeKey,
				mstrConsumeSecret, mstrRequestToken, mstrRequestTokenSecret,
				strOauthVerifier);
		if (MiscHelper.isEmpty(strAccessToken) == true)
			return null;

		String[] tokenArray = strAccessToken.split("&");
		if (tokenArray.length < 2) {
			return null;
		}

		String strTokenKey = tokenArray[0];
		String strTokenSecrect = tokenArray[1];
		String[] token1 = strTokenKey.split("=");
		if (token1.length < 2) {
			return null;
		}

		String[] token2 = strTokenSecrect.split("=");
		if (token2.length < 2) {
			return null;
		}

		mstrAccessToken = token1[1];
		mstrAccessTokenSecret = token2[1];

		return mstrAccessToken;
	}
}
